package caidan.data;

import java.awt.GridLayout;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;

import caidan.SearchDialog;
import caidan.data.FlavourPane.FlavourContainer;

public class FoodMaterial
{
	Material mainMaterial = Material.unChooseMaterial;
	private Material subMaterial1 = Material.unChooseMaterial;
	private Material subMaterial2 = Material.unChooseMaterial;
	private Material subMaterial3 = Material.unChooseMaterial;
	private short mainQuality, subQuality1, subQuality2, subQuality3;

	private Flavour flavour1 = Flavour.unChooseFlavour;
	private Flavour flavour2 = Flavour.unChooseFlavour;
	private Flavour flavour3 = Flavour.unChooseFlavour;
	private Flavour flavour4 = Flavour.unChooseFlavour;
	private short flaQuality1, flaQuality2, flaQuality3, flaQuality4;

	public FoodMaterial()
	{
	}

	public boolean isMainMaterialSetted()
	{
		return mainMaterial != Material.unChooseMaterial;
	}

	public void setFoodMaterial(FoodMaterial foodMaterial)
	{
		mainMaterial = foodMaterial.mainMaterial;
		subMaterial1 = foodMaterial.subMaterial1;
		subMaterial2 = foodMaterial.subMaterial2;

		mainQuality = foodMaterial.mainQuality;
		subQuality1 = foodMaterial.subQuality1;
		subQuality2 = foodMaterial.subQuality2;

		flavour1 = foodMaterial.flavour1;
		flavour2 = foodMaterial.flavour2;
		flavour3 = foodMaterial.flavour3;
		flavour4 = foodMaterial.flavour4;

		flaQuality1 = foodMaterial.flaQuality1;
		flaQuality2 = foodMaterial.flaQuality2;
		flaQuality3 = foodMaterial.flaQuality3;
		flaQuality4 = foodMaterial.flaQuality4;
	}

	public void clear()
	{
		mainMaterial = Material.unChooseMaterial;
		subMaterial1 = Material.unChooseMaterial;
		subMaterial2 = Material.unChooseMaterial;

		mainQuality = 0;
		subQuality1 = 0;
		subQuality2 = 0;

		flavour1 = Flavour.unChooseFlavour;
		flavour2 = Flavour.unChooseFlavour;
		flavour3 = Flavour.unChooseFlavour;
		flavour4 = Flavour.unChooseFlavour;

		flaQuality1 = 0;
		flaQuality2 = 0;
		flaQuality3 = 0;
		flaQuality4 = 0;
	}

	void writeData(DataOutputStream dos) throws IOException
	{
		mainMaterial.writeData(dos);
		dos.writeShort(mainQuality);
		subMaterial1.writeData(dos);
		dos.writeShort(subQuality1);
		subMaterial2.writeData(dos);
		dos.writeShort(subQuality2);
		subMaterial3.writeData(dos);
		dos.writeShort(subQuality3);

		flavour1.writeData(dos);
		dos.writeShort(flaQuality1);
		flavour2.writeData(dos);
		dos.writeShort(flaQuality2);
		flavour3.writeData(dos);
		dos.writeShort(flaQuality3);
		flavour4.writeData(dos);
		dos.writeShort(flaQuality4);
	}

	void readData(DataInputStream dis, int appVersion) throws IOException
	{
		mainMaterial = Material.readData(dis, appVersion);
		mainQuality = dis.readShort();
		subMaterial1 = Material.readData(dis, appVersion);
		subQuality1 = dis.readShort();
		subMaterial2 = Material.readData(dis, appVersion);
		subQuality2 = dis.readShort();
		if (appVersion >= 306)
		{
			subMaterial3 = Material.readData(dis, appVersion);
			subQuality3 = dis.readShort();
		}

		flavour1 = Flavour.readData(dis, appVersion);
		flaQuality1 = dis.readShort();
		flavour2 = Flavour.readData(dis, appVersion);
		flaQuality2 = dis.readShort();
		flavour3 = Flavour.readData(dis, appVersion);
		flaQuality3 = dis.readShort();
		flavour4 = Flavour.readData(dis, appVersion);
		flaQuality4 = dis.readShort();
	}

	public static class FoodMaterialPanel extends JPanel
	{
		private static final long serialVersionUID = 917532838017751653L;
		private MaterialPane mainMaterialPane;
		private MaterialPane subMaterialPane1;
		private MaterialPane subMaterialPane2;
		private MaterialPane subMaterialPane3;
		private FlavourPane flavourPane1;
		private FlavourPane flavourPane2;
		private FlavourPane flavourPane3;
		private FlavourPane flavourPane4;

		private SearchDialog searchDialog;

		private FoodMaterial foodMaterial;

		public FoodMaterialPanel()
		{
			super(new GridLayout(0, 1, 0, 3));
			mainMaterialPane = new MaterialPane(this, "主材料：", (Material o) ->
			{
				foodMaterial.mainMaterial = o;
			});
			mainMaterialPane.getQualitySpinner()
					.addChangeListener((ChangeEvent e) ->
					{
						foodMaterial.mainQuality = mainMaterialPane
								.getQuality();
					});

			subMaterialPane1 = new MaterialPane(this, "副材料：", (Material o) ->
			{
				foodMaterial.subMaterial1 = o;
			});
			subMaterialPane1.getQualitySpinner()
					.addChangeListener((ChangeEvent e) ->
					{
						foodMaterial.subQuality1 = subMaterialPane1
								.getQuality();
					});

			subMaterialPane2 = new MaterialPane(this, "副材料：", (Material o) ->
			{
				foodMaterial.subMaterial2 = o;
			});
			subMaterialPane2.getQualitySpinner()
					.addChangeListener((ChangeEvent e) ->
					{
						foodMaterial.subQuality2 = subMaterialPane2
								.getQuality();
					});

			subMaterialPane3 = new MaterialPane(this, "副材料：", (Material o) ->
			{
				foodMaterial.subMaterial3 = o;
			});
			subMaterialPane3.getQualitySpinner()
					.addChangeListener((ChangeEvent e) ->
					{
						foodMaterial.subQuality3 = subMaterialPane3
								.getQuality();
					});

			flavourPane1 = new FlavourPane((Flavour o) ->
			{
				foodMaterial.flavour1 = o;
			});
			flavourPane1.getQualitySpinner()
					.addChangeListener((ChangeEvent e) ->
					{
						foodMaterial.flaQuality1 = flavourPane1.getQuality();
					});

			flavourPane2 = new FlavourPane((Flavour o) ->
			{
				foodMaterial.flavour2 = o;
			});
			flavourPane2.getQualitySpinner()
					.addChangeListener((ChangeEvent e) ->
					{
						foodMaterial.flaQuality2 = flavourPane2.getQuality();
					});

			flavourPane3 = new FlavourPane((Flavour o) ->
			{
				foodMaterial.flavour3 = o;
			});
			flavourPane3.getQualitySpinner()
					.addChangeListener((ChangeEvent e) ->
					{
						foodMaterial.flaQuality3 = flavourPane3.getQuality();
					});

			flavourPane4 = new FlavourPane((Flavour o) ->
			{
				foodMaterial.flavour4 = o;
			});
			flavourPane4.getQualitySpinner()
					.addChangeListener((ChangeEvent e) ->
					{
						foodMaterial.flaQuality4 = flavourPane4.getQuality();
					});

			add(mainMaterialPane);
			add(subMaterialPane1);
			add(subMaterialPane2);
			add(subMaterialPane3);
			add(new FlavourContainer(flavourPane1, flavourPane2));
			add(new FlavourContainer(flavourPane3, flavourPane4));
		}

		public SearchDialog getSearchDialog()
		{
			if (searchDialog == null)
				searchDialog = new SearchDialog(
						SwingUtilities.getWindowAncestor(this));
			return searchDialog;
		}

		public void setFoodMaterial(FoodMaterial foodMaterial)
		{
			this.foodMaterial = foodMaterial;
			mainMaterialPane.setMaterial(foodMaterial.mainMaterial);
			mainMaterialPane.setQuality(foodMaterial.mainQuality);
			subMaterialPane1.setMaterial(foodMaterial.subMaterial1);
			subMaterialPane1.setQuality(foodMaterial.subQuality1);
			subMaterialPane2.setMaterial(foodMaterial.subMaterial2);
			subMaterialPane2.setQuality(foodMaterial.subQuality2);
			subMaterialPane3.setMaterial(foodMaterial.subMaterial3);
			subMaterialPane3.setQuality(foodMaterial.subQuality3);

			flavourPane1.setFlavour(foodMaterial.flavour1);
			flavourPane1.setQuality(foodMaterial.flaQuality1);
			flavourPane2.setFlavour(foodMaterial.flavour2);
			flavourPane2.setQuality(foodMaterial.flaQuality2);
			flavourPane3.setFlavour(foodMaterial.flavour3);
			flavourPane3.setQuality(foodMaterial.flaQuality3);
			flavourPane4.setFlavour(foodMaterial.flavour4);
			flavourPane4.setQuality(foodMaterial.flaQuality4);
		}
	}
}
